package com.itheima.controller;

import com.itheima.domain.Answer;
import com.itheima.domain.Book;
import com.itheima.domain.Question;
import com.itheima.domain.User;
import com.itheima.service.AnswerService;
import com.itheima.service.BookService;
import com.itheima.service.QuestionService;
import com.itheima.service.UserService;
import com.itheima.until.Code;
import com.itheima.until.Encryptor;
import com.itheima.until.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Random;

@Controller
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private UserService userService;
    @Autowired
    private QuestionService questionService;
    @Autowired
    private AnswerService answerService;

    @RequestMapping("/users")
    @ResponseBody
    public Result getAllUsers(String loginname) {
        System.out.println("loginname = " + loginname);
        if (loginname == null){
            List<User> users = userService.getAll();
            Integer code = users != null ? Code.GET_OK : Code.GET_ERR;
            String msg = users != null ? "" : "数据查询失败，请重试！";
            return new Result(code,users,msg);
        }
        List<User> users = userService.selectAlluser(loginname);

        Integer code = users != null ? Code.GET_OK : Code.GET_ERR;
        String msg = users != null ? "" : "数据查询失败，请重试！";
        return new Result(code,users,msg);
    }

    @RequestMapping("/user")
    @ResponseBody
    //为什么不能用Integer
    public Result user(@RequestParam("uid") int uid) {
        User user = userService.getById(uid);

        Integer code = user != null ? Code.GET_OK : Code.GET_ERR;
        String msg = user != null ? "" : "数据查询失败，请重试！";
        return new Result(code,user,msg);
    }

//    @RequestMapping("/userUpdate")
//    @ResponseBody
//    public Result userUpdate(@RequestBody User user) {
////        User u = userService.getById(user.getUid());
//
//        boolean flag = userService.update(user);
//        return new Result(flag ? Code.UPDATE_OK:Code.UPDATE_ERR,flag);
//    }

    @RequestMapping("/limit")
    @ResponseBody
    public Result limit(int uid) {
        int temp = 1;

        int identity = userService.getIdentity(uid);
        temp -=identity;
        boolean flag = userService.limit(uid, temp);
        String msg = identity == 1 ? "禁用成功！" : "已解封！";
        return new Result(flag ? Code.UPDATE_OK:Code.UPDATE_ERR,flag,msg);
    }

    @RequestMapping("/ques")
    @ResponseBody
    public Result getAllQues(String title) {
        if (title == null){
            title = " ";
        }
        System.out.println("title = " + title);
        List<Question> questions = questionService.getAllQues(title);
        for(Question question:questions){
            Integer uid = question.getUid();
            User user = userService.getById(uid);
            question.setUser(user);
        }
        Integer code = questions != null ? Code.GET_OK : Code.GET_ERR;
        String msg = questions != null ? "" : "数据查询失败，请重试！";
        return new Result(code,questions,msg);
    }
    @RequestMapping("/up")
    @ResponseBody
    public Result up(int qid) {
        int status = 1;
        Boolean flag = questionService.updateStatus(status, qid);
        return new Result(flag ? Code.DELETE_OK:Code.DELETE_ERR,flag);
    }
    @RequestMapping("/down")
    @ResponseBody
    public Result down(int qid) {
        int status = 0;
        Boolean flag = questionService.updateStatus(status, qid);
        return new Result(flag ? Code.DELETE_OK:Code.DELETE_ERR,flag);
    }
    @RequestMapping("/deleteQues")
    @ResponseBody
    public Result deleteQues(int qid) {

        Boolean flag = questionService.delete(qid);
        return new Result(flag ? Code.DELETE_OK:Code.DELETE_ERR,flag);
    }

    @RequestMapping("/getAllAnswers")
    @ResponseBody
    public Result getAllAnswers() {

        List<Answer> answers = answerService.getAll();
        for(Answer answer:answers){
            Integer uid = answer.getUid();
            User user = userService.getById(uid);
            answer.setUser(user);
            Integer qid = answer.getQid();
            Question question = questionService.getById(qid);
            answer.setQuestion(question);
        }
        Integer code = answers != null ? Code.GET_OK : Code.GET_ERR;
        String msg = answers != null ? "" : "数据查询失败，请重试！";
        return new Result(code,answers,msg);
    }
    @RequestMapping("/deleteAnswer")
    @ResponseBody
    public Result deleteAnswer(int aid) {

        Boolean flag = answerService.delete(aid);
        return new Result(flag ? Code.DELETE_OK:Code.DELETE_ERR,flag);
    }
    @RequestMapping("/removeUser")
    @ResponseBody
        public Result removeUser(Integer uid) {
        User user = new User();
        user.setUid(uid);
        user.setPassword(Encryptor.encrypt("123"));
        user.setAvatar("res/images/uer.jpg");
        String nickname = "浦江用户"+ System.currentTimeMillis();
        user.setNickname(nickname);

        Boolean flag = userService.update(user);

        return new Result(flag ? Code.UPDATE_OK:Code.UPDATE_ERR,flag);
    }
}
